home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / librw / RWAuditStreamBuffer.z / RWAuditStreamBuffer
Encoding:
Text File  |  2002-10-03  |  14.7 KB  |  331 lines

  1.  
  2.  
  3.  
  4. RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))                            RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))
  5.  
  6.  
  7.  
  8. NNNNaaaammmmeeee
  9.      RWAuditStreamBuffer - Rogue Wave library class
  10.  
  11. SSSSyyyynnnnooooppppssssiiiissss
  12.               #include <rw/auditbuf.h>
  13.  
  14.  
  15.  
  16.               #include <iostream.h>
  17.           RWAuditStreamBuffer buf(arguments)
  18.           ostream os(&buf); // may be used for ostreams
  19.           istream is(&buf); // or istreams of any kind
  20.  
  21.  
  22.  
  23. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn
  24.      Class RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr is used to construct a stream, after which the
  25.      RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr instance will count all the bytes that pass through
  26.      the stream.  If constructed with a function pointer, RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr
  27.      will call that function with each byte that passes through the stream.
  28.      The counting capacity provides for streams the equivalent of the
  29.      RRRRWWWWCCCCoooolllllllleeeeccccttttaaaabbbblllleeee method rrrreeeeccccuuuurrrrssssiiiivvvveeeeSSSSttttoooorrrreeeeSSSSiiiizzzzeeee(((()))) which is only available for
  30.      RRRRWWWWFFFFiiiilllleeee.
  31.  
  32. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee
  33.      None
  34.  
  35. SSSShhhhoooorrrrtttt EEEExxxxaaaammmmpppplllleeee
  36.               #include <rw/auditbuf.h>
  37.  
  38.  
  39.  
  40.               #include <rw/bstream.h>
  41.           #include <rw/pstream.h>
  42.           #include <iostream.h>
  43.           int main() {
  44.             RWCollectable ct;
  45.             fillCollectable();  // make a collection, somehow
  46.             RWAuditStreamBuffer bcounter, pcounter;
  47.             RWbostream bcount(&bcounter); //ctor takes streambuf pointer
  48.             RWpostream pcount(&pcounter);
  49.           //_
  50.             bcount << ct;
  51.             pcount << ct;
  52.           cout  << "We just counted " << bcounter
  53.                 << " bytes from an RWbostream." << endl;
  54.           cout  << "We just counted " << pcounter
  55.                 << " bytes from an RWpostream." << endl;
  56.           return 0;
  57.           }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))                            RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))
  71.  
  72.  
  73.  
  74. RRRReeeellllaaaatttteeeedddd CCCCllllaaaasssssssseeeessss
  75.      RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr may be used as the streambuf for any stream,
  76.      including those derived from RRRRWWWWvvvvoooossssttttrrrreeeeaaaammmm or RRRRWWWWvvvviiiissssttttrrrreeeeaaaammmm, ssssttttrrrrssssttttrrrreeeeaaaammmm,
  77.      iiiiffffssssttttrrrreeeeaaaammmm, ooooffffssssttttrrrreeeeaaaammmm, etc.
  78.  
  79. GGGGlllloooobbbbaaaallll TTTTyyyyppppeeeeddddeeeeffff
  80.               typedef void (*RWauditFunction)(unsigned char, void*);
  81.  
  82.  
  83.      If you wish to do more than count each character handled by the buffer,
  84.      you may provide an RRRRWWWWaaaauuuuddddiiiittttFFFFuuuunnnnccccttttiiiioooonnnn to the constructor. The first
  85.      parameter to this function is a byte provided by the stream.  The second
  86.      parameter is the address of the conter to be manipulated by
  87.      RRRRWWWWAAAAuuuuddddiiiittttFFFFuuuunnnnccccttttiiiioooonnnn.
  88.  
  89. PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  90.               RWAuditStreamBuffer(RWauditFunction=0, void*=0);
  91.  
  92.  
  93.      Constructs a new RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr that may be used only to examine and
  94.      count every byte that passes into an oooossssttttrrrreeeeaaaammmm that has the
  95.      RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr instance as its ssssttttrrrreeeeaaaammmmbbbbuuuuffff. It will not forward the
  96.      bytes to any stream, nor accept bytes from a stream. The second argument
  97.      to the constructor allows you to supply storage for the byte count. It is
  98.      optional.
  99.  
  100.               RWAuditStreamBuffer(istream&, RWauditFunction=0, void*=0);
  101.  
  102.  
  103.      Constructs a new RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr that passes bytes from the iiiissssttttrrrreeeeaaaammmm
  104.      on which it is constructed to the iiiissssttttrrrreeeeaaaammmm that has the
  105.      RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr instance as its ssssttttrrrreeeeaaaammmmbbbbuuuuffff.  A typical use would be to
  106.      count or examine the bytes being input from a file through a stream
  107.      derived from RRRRWWWWvvvviiiissssttttrrrreeeeaaaammmm. The second argument to the constructor allows
  108.      you to supply storage for the byte count. It is optional.
  109.  
  110.               RWAuditStreamBuffer(iostream&, RWauditFunction=0, void*=0);
  111.  
  112.  
  113.      Constructs a new RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr that passes bytes to and from the
  114.      ioooossssttttrrrreeeeaaaammmm on which it is constructed to and from the iiiissssttttrrrreeeeaaaammmm that has the
  115.      RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr instance as its ssssttttrrrreeeeaaaammmmbbbbuuuuffff.  A typical use would be to
  116.      count or examine the bytes being transferred to and from a file used to
  117.      store and retrieve changing data. The second argument to the constructor
  118.      allows you to supply storage for the byte count. It is optional.
  119.  
  120.               RWAuditStreamBuffer(ostream&, RWauditFunction=0, void*=0);
  121.  
  122.  
  123.      Constructs a new RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr that passes bytes into the oooossssttttrrrreeeeaaaammmm
  124.      on which it is constructed from the oooossssttttrrrreeeeaaaammmm that has the
  125.      RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr instance as its ssssttttrrrreeeeaaaammmmbbbbuuuuffff.  A typical use would be to
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))                            RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))
  137.  
  138.  
  139.  
  140.      count or examine the bytes being output to a file through a stream
  141.      derived from RRRRWWWWvvvvoooossssttttrrrreeeeaaaammmm. The second argument to the constructor allows
  142.      you to supply storage for the byte count. It is optional.
  143.  
  144.               RWAuditStreamBuffer(streambuf*, RWauditFunction=0, void*=0);
  145.  
  146.  
  147.      Constructs a new RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr that passes bytes into the oooossssttttrrrreeeeaaaammmm
  148.      on which it is constructed from the oooossssttttrrrreeeeaaaammmm that has the
  149.      RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr instance as its ssssttttrrrreeeeaaaammmmbbbbuuuuffff.  A typical use would be to
  150.      count or examine the bytes being output to a file through a stream
  151.      derived from RRRRWWWWvvvvoooossssttttrrrreeeeaaaammmm. The second argument to the constructor allows
  152.      you to supply storage for the byte count. It is optional.
  153.  
  154. PPPPuuuubbbblllliiiicccc DDDDeeeessssttttrrrruuuuccccttttoooorrrr
  155.               virtual ~RWAuditStreamBuffer();
  156.  
  157.  
  158.      We have provided an empty destructor since some compilers complain if
  159.      there is no virtual destructor for a class that has virtual methods.
  160.  
  161. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr OOOOppppeeeerrrraaaattttoooorrrr
  162.               ooooppppeeeerrrraaaattttoooorrrr unsigned long();
  163.  
  164.  
  165.      Provides the count of bytes seen so far.
  166.  
  167. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnn
  168.               unsigned long
  169.           rrrreeeesssseeeetttt(unsigned long value = 0);
  170.  
  171.  
  172.      Resets the count of bytes seen so far.  Returns the current count.
  173.  
  174. EEEExxxxtttteeeennnnddddeeeedddd EEEExxxxaaaammmmpppplllleeee
  175.               #include <iostream.h>
  176.  
  177.  
  178.  
  179.               #include <fstream.h>
  180.           #include <rw/auditbuf.h>
  181.           #include <rw/pstream.h>
  182.           #include <rw/cstring.h>
  183.           void doCrc (unsigned char c, void* x) {
  184.             *(unsigned char*)x ^= c;
  185.           }
  186.           int main() {
  187.           if(1) { // just a block to control variable lifetime
  188.               unsigned char check = ' ';
  189.               // create an output stream
  190.               ofstream                            op("crc.pst");
  191.               // create an RWAuditStreamBuffer that will do CRC
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))                            RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))
  203.  
  204.  
  205.  
  206.               RWAuditStreamBuffer               crcb(op,doCrc,&check);
  207.               // create an RWpostream to put the data through.
  208.           RWpostream                           p(&crcb);
  209.               // now send some random stuff to the stream
  210.               p << RWCString("The value of Tools.h++ is at least ");
  211.               p << (int)4;
  212.               p << RWCString(" times that of the next best library!0) ;
  213.               p << RWCString("Pi is about ") << (double)3.14159 << '.';
  214.               // finally, save the sum on the stream itself.
  215.           p << (unsigned int)check; // alters check, _after_ saving it...
  216.               // just for fun, print out some statistics:
  217.               cout << "We just saved " << crcb
  218.                    << " bytes of data to the file." << endl;
  219.               cout << "The checksum for those bytes was " <<check << endl;
  220.           } // end of block
  221.             // now read the data back in, checking to see if it survived.
  222.             unsigned char check = ' ';
  223.             // create an instream
  224.             ifstream                            ip("crc.pst");
  225.             // create an RWAuditStreamBuffer that will do CRC
  226.             RWAuditStreamBuffer               crcb(ip,doCrc,&check);
  227.             // create an RWpistream to interpret the bytes
  228.             RWpistream                           p(&crcb);
  229.             RWCString first, mid1, mid2;
  230.             int value;
  231.             double pi;
  232.             char pnc;
  233.             unsigned int savedCRC;
  234.             unsigned char matchCRC;
  235.             // read in the data. Don't read the checksum yet!
  236.             p >> first >> value >> mid1 >> mid2 >> pi >> pnc;
  237.             // save the checksum
  238.             matchCRC = check;
  239.             // Now it is safe to alter the running checksum by reading in
  240.             // the one saved in the file.
  241.           p >> savedCRC;
  242.             if(savedCRC != matchCRC) {
  243.               cout << "Checksum error. Saved CRC: " << savedCRC
  244.                    << " built CRC: " << matchCRC << dec << endl;
  245.             }
  246.             else {
  247.               cout << "The message was: " << endl;
  248.               cout << first << value << mid1 << mid2 << pi << pnc << endl;
  249.             }
  250.             // just for fun, print out some statistics:
  251.             cout  << "We just read " << crcb
  252.                   << " bytes of data from the file." << endl;
  253.             cout  << "The checksum was " << matchCRC << flush;
  254.             cout  << " and the saved checksum was " << savedCRC << endl;
  255.           return 0;
  256.           }
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))                            RRRRWWWWAAAAuuuuddddiiiittttSSSSttttrrrreeeeaaaammmmBBBBuuuuffffffffeeeerrrr((((3333CCCC++++++++))))
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.                                                                         PPPPaaaaggggeeee 5555
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.